package com.tangly.codeing.service.impl;

import com.tangly.codeing.dao.${classInfo.className}Dao;
import com.tangly.codeing.model.${classInfo.className};
import com.tangly.codeing.service.${classInfo.className}Service;
import com.tangly.codeing.utils.IdWoker;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ${classInfo.classComment}
 * @author tangbing
 * @date ${.now?string('yyyy-MM-dd')}
 */
@Service
public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service {

	@Resource
	private ${classInfo.className}Dao ${classInfo.className?uncap_first}Dao;
	@Resource
	private IdWoker idWoker;

	/**
    * 新增
    */
	@Override
	public ReturnT<String> insert(${classInfo.className} ${classInfo.className?uncap_first}) {

		// valid
		if (${classInfo.className?uncap_first} == null) {
			return new ReturnT<>(ReturnT.FAIL_CODE, "必要参数缺失");
        }
		${classInfo.className?uncap_first}.setUuid(idWoker.nextStringId());
		${classInfo.className?uncap_first}Dao.insert(${classInfo.className?uncap_first});
        return ReturnT.SUCCESS;
	}

	/**
	* 删除
	*/
	@Override
	public ReturnT<String> delete(Long id) {
		int ret = ${classInfo.className?uncap_first}Dao.delete(id);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	/**
	* 批量删除
	*/
	@Override
	public ReturnT<String> batchDelete(List<String> ids) {
		int ret = ${classInfo.className?uncap_first}Dao.batchDelete(ids);
		return ret > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
	}

	/**
	* 更新
	*/
	@Override
	public ReturnT<String> update(${classInfo.className} ${classInfo.className?uncap_first}) {
		int ret = ${classInfo.className?uncap_first}Dao.update(${classInfo.className?uncap_first});
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	/**
	* Load查询
	*/
	@Override
	public ${classInfo.className} load(Long id) {
		return ${classInfo.className?uncap_first}Dao.load(id);
	}

	/**
	* 分页查询
	*/
	@Override
	public PageInfo pageList(PageInfo pageInfo) {

		List<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Dao.pageList(pageInfo);
		int totalCount = ${classInfo.className?uncap_first}Dao.pageListCount(pageInfo);

        pageInfo.setResultObject(pageList);
        pageInfo.setTotalRecord(totalCount);
        return pageInfo;
	}

}
